package com.limelight.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.TextureView;


public class SBSStreamView extends TextureView {
   public SBSStreamView(Context context) {
      super(context);
   }

   public SBSStreamView(Context context, AttributeSet attrs) {
      super(context, attrs);
   }

   public SBSStreamView(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
   }

   private double desiredAspectRatio;
   private SBSStreamView.InputCallbacks inputCallbacks;

   public void setDesiredAspectRatio(double aspectRatio) {
      this.desiredAspectRatio = aspectRatio;
   }

   public void setInputCallbacks(SBSStreamView.InputCallbacks callbacks) {
      this.inputCallbacks = callbacks;
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      // If no fixed aspect ratio has been provided, simply use the default onMeasure() behavior
      if (desiredAspectRatio == 0) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         return;
      }

      // Based on code from: https://www.buzzingandroid.com/2012/11/easy-measuring-of-custom-views-with-specific-aspect-ratio/
      int widthSize = MeasureSpec.getSize(widthMeasureSpec);
      int heightSize = MeasureSpec.getSize(heightMeasureSpec);

      int measuredHeight, measuredWidth;
      if (widthSize > heightSize * desiredAspectRatio) {
         measuredHeight = heightSize;
         measuredWidth = (int)(measuredHeight * desiredAspectRatio);
      } else {
         measuredWidth = widthSize;
         measuredHeight = (int)(measuredWidth / desiredAspectRatio);
      }

      setMeasuredDimension(measuredWidth, measuredHeight);
   }

   @Override
   public boolean onKeyPreIme(int keyCode, KeyEvent event) {
      // This callbacks allows us to override dumb IME behavior like when
      // Samsung's default keyboard consumes Shift+Space.
      if (inputCallbacks != null) {
         if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (inputCallbacks.handleKeyDown(event)) {
               return true;
            }
         }
         else if (event.getAction() == KeyEvent.ACTION_UP) {
            if (inputCallbacks.handleKeyUp(event)) {
               return true;
            }
         }
      }

      return super.onKeyPreIme(keyCode, event);
   }

   public interface InputCallbacks {
      boolean handleKeyUp(KeyEvent event);
      boolean handleKeyDown(KeyEvent event);
   }


}
